package com.example.ui.contact.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.example.entity.ShareEntity;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.ui.contact.bean.NoticeItem;
import com.example.ui.contact.bean.ShareItem;
import com.example.ui.contact.chat.L;
import com.example.ui.contact.face.FaceUtil;
import com.example.ui.homepage.HomePageView;
import com.example.ui.leftmenu.Item08View;
import com.example.util.BitmapLoad;
import com.example.util.Util;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class PersonAdapter extends BaseAdapter {
	Context context;
	//List<Map<String, Object>> listData;
	List<ShareItem> msgList;
	String imgPath = null;
	String time = null;
	String[] originalImgArr;
	String[] transitionImgArr;
	int typeID;
	String headPath;
    FaceUtil faceUtil = HSApplication.getInstance().getFaceUtil();
	public PersonAdapter(Context mContext,List<ShareItem> msgList){
		context=mContext;
		this.msgList = msgList;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return msgList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint({ "UseValueOf", "SimpleDateFormat" })
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		convertView=null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.personhome_listview_item, null);
			holder = new ViewHolder();
			
			holder.headImg=(ImageView) convertView.findViewById(R.id.personhome_listview_item_head);
			holder.contentLayout=(LinearLayout)convertView.findViewById(R.id.personhome_listview_item_content_layout);
			holder.name=(TextView) convertView.findViewById(R.id.personhome_listview_item_name);
			holder.title=(TextView) convertView.findViewById(R.id.personhome_listview_item_title);
			holder.contentText=(TextView) convertView.findViewById(R.id.personhome_listview_item_content_text);
			holder.content_photo1=(ImageView) convertView.findViewById(R.id.personhome_listview_item_content_photo1);
			holder.content_photo2=(ImageView) convertView.findViewById(R.id.personhome_listview_item_content_photo2);
			holder.content_photo3=(ImageView) convertView.findViewById(R.id.personhome_listview_item_content_photo3);
			holder.time=(TextView) convertView.findViewById(R.id.personhome_listview_item_time);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		ShareItem shareItem = Item08View.share_list.get(position);
		L.Debug("shareItem.getNickname()", shareItem.getNickname());
		L.Debug("share_list length", ""+Item08View.share_list.size());
		try {
//			Bitmap headBit = BitmapLoad.loadBitmap(context, headPath);
			holder.headImg.setBackgroundResource(R.drawable.head_bg);//头像
			holder.name.setText(shareItem.getNickname());//名字    "小夏"+position 
			holder.title.setText("这里显示的是文章的主题");
//			holder.contentText.setText(faceUtil.convertNormalStringToSpannableString(shareItem.getTxt()),
//					BufferType.SPANNABLE);//shareItem.getTxt()
			holder.contentText.setText("这里是主要内容，展示最多500个字，下面是默认显示的三张图片（若包含3张及以上）,否则只显示一张或两张，" +
					"剩余的设置不显示,这里默认显示的是照片墙的背景图片，宽100dp，高90dp");
			holder.content_photo1.setBackgroundResource(R.drawable.wall_bg);//头像
			holder.content_photo2.setBackgroundResource(R.drawable.wall_bg);//头像
			holder.content_photo3.setBackgroundResource(R.drawable.wall_bg);//头像
			
			long testCurTime = System.currentTimeMillis()/1000;//测试专用时间转换（2013-08-01--》1375335011）
			
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				long dataTime = testCurTime;
				time = format.format(dataTime*1000);
				holder.time.setText(time==""?"":Util.DateCompare(time));//时间
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return convertView;
	}

//	private void originalImageView(ViewHolder holder,String imgPath) {
//		
//		if(imgPath!=null){
//			try {
//				JSONArray jsonArray = new JSONArray(imgPath);
//				originalImgArr = new String[jsonArray.length()];
//				for(int i=0;i<jsonArray.length();i++){
//					JSONObject json = jsonArray.getJSONObject(i);
//					originalImgArr[i] = json.getString("pic");
//				}
//				switch (originalImgArr.length) {
//				case 0:
//					holder.content_photo1.setVisibility(View.GONE);
//					holder.content_photo2.setVisibility(View.GONE);
//					holder.content_photo3.setVisibility(View.GONE);
//					break;
//				case 1:
//					holder.content_photo1.setImageBitmap(BitmapLoad.loadBitmap(context, originalImgArr[0]));
//					holder.content_photo2.setVisibility(View.GONE);
//					holder.content_photo3.setVisibility(View.GONE);
//					break;
//				case 2:
//					holder.content_photo1.setImageBitmap(BitmapLoad.loadBitmap(context, originalImgArr[0]));
//					holder.content_photo2.setImageBitmap(BitmapLoad.loadBitmap(context, originalImgArr[1]));
//					holder.content_photo3.setVisibility(View.GONE);
//					break;
//				default:
//					holder.content_photo1.setImageBitmap(BitmapLoad.loadBitmap(context, originalImgArr[0]));
//					holder.content_photo2.setImageBitmap(BitmapLoad.loadBitmap(context, originalImgArr[1]));
//					holder.content_photo3.setImageBitmap(BitmapLoad.loadBitmap(context, originalImgArr[2]));
//					break;
//				}
//			} catch (JSONException e) {
//				Util.toast(context, "图片加载失败");
//				e.printStackTrace();
//			}
//		}else{
//			holder.content_photo1.setVisibility(View.GONE);
//			holder.content_photo2.setVisibility(View.GONE);
//			holder.content_photo3.setVisibility(View.GONE);
//		}
//	}
	

	class ViewHolder {
		//不变的
		ImageView headImg;//头像
		LinearLayout contentLayout;//布局
		TextView name;//名字
		TextView title;//标题
		TextView contentText;//内容 文字
		ImageView content_photo1;//图片1
		ImageView content_photo2;//图片2
		ImageView content_photo3;//图片3
		TextView time;//时间
		
	}

}